package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.ClassificationMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.ClassificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ClassificationServiceImpl implements ClassificationService {
    @Autowired
    private ClassificationMapper classificationMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult selectAllCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        List<Category> list = classificationMapper.selectAllCategory(categoryPageQueryDTO);
        Page<Category> page = (Page<Category>) list;
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void insertCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateTime(LocalDateTime.now());
        Long id = BaseContext.getCurrentId();
        log.info("登陆人ID："+id);
        category.setCreateUser(id);
        category.setUpdateUser(id);
        classificationMapper.insertCategory(category);
    }

    @Override
    public void deleteCategory(Long id) {
        Integer type = classificationMapper.selectById(id);
        // DO 查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Long categoryId = id;
        if(type == 1){
            List<Dish> dishList = dishMapper.selectDishById(categoryId);
            if(dishList != null && dishList.size()>0 ){
                throw new DeletionNotAllowedException("该分类下有菜品，不能删除");
            }
        }else if (type == 2){
            List<Setmeal> SetmealList =  setmealMapper.selectDishById(categoryId);
            if(SetmealList != null &&  SetmealList.size()>0 ){
                throw new DeletionNotAllowedException("该分类下有套餐，不能删除");
            }
        }

        // DO 查询当前分类是否关联了套餐，如果关联了就抛出业务异常

        classificationMapper.deleteCategory(id);
    }

    @Override
    public void updaateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        category.setUpdateTime(LocalDateTime.now());
        Long id = BaseContext.getCurrentId();
        category.setUpdateUser(id);
        BeanUtils.copyProperties(categoryDTO,category);
        classificationMapper.updateCategory(category);
    }

    @Override
    public void updateCategory(Integer status, Long id) {
        classificationMapper.updateStatusById(status,id);
    }

    @Override
    public List<Category> selectCategoryByType(Integer type) {
         List<Category> list = classificationMapper.selectCategoryByType(type);
        return list;
    }
}